from google.appengine.api import users
from models import User

def get_current_user ():
	google_user = users.get_current_user ()
	if google_user == None: return None
	query = User.all ()
	query.filter ("google_user =", google_user)
	results = query.fetch (1)
	if results == []: return create_new_user (google_user)
	else: return results[0]

def create_new_user (google_user):
	user = User ()
	user.google_user = google_user
	user.display_name = google_user.nickname().split("@")[0]
	user.put ()
	return user
